Imports System.Collections.Generic
Namespace Blog
    Public Class Post

        Dim _Id As Integer

        'The basic info pertaining to a blog posting
        Dim _Title As String
        Dim _Body As String
        Dim _DateAdded As Date
        Dim _DateUpdated As Date

        'Special blog features
        Dim _Mood As Integer
        Dim _Public As Boolean
        Dim _Comments As List(Of Comment)

        Sub New()
            _Comments = New List(Of Comment)
        End Sub

        Public Property Id() As Integer
            Get
                Return _Id
            End Get
            Set(ByVal value As Integer)
                _Id = value
            End Set
        End Property

        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property

        Public Property Body() As String
            Get
                Return _Body
            End Get
            Set(ByVal value As String)
                _Body = value
            End Set
        End Property

        Public Property DateAdded() As Date
            Get
                Return _DateAdded
            End Get
            Set(ByVal value As Date)
                _DateAdded = value
            End Set
        End Property

        Public Property DateUpdated() As Date
            Get
                Return _DateUpdated
            End Get
            Set(ByVal value As Date)
                _DateUpdated = value
            End Set
        End Property

        Public Property Mood() As Integer
            Get
                Return _Mood
            End Get
            Set(ByVal value As Integer)
                _Mood = value
            End Set
        End Property

        Public Property [Public]() As Boolean
            Get
                Return _Public
            End Get
            Set(ByVal value As Boolean)
                _Public = value
            End Set
        End Property

        Public ReadOnly Property Comments() As List(Of Comment)
            Get
                Return _Comments
            End Get
        End Property

        ' Derivived properties
        Public ReadOnly Property HasBeenUpdated() As Boolean
            Get
                If Date.Compare(_DateAdded, _DateUpdated) = 0 Then Return False
                Return True
            End Get
        End Property

        Public ReadOnly Property TotalRating() As Integer
            Get
                Dim total As Integer = 0
                For Each comment As Comment In _Comments
                    total += comment.Rating
                Next
                Return total
            End Get
        End Property

        'Comparers for sorting
        Public Class SortByMostRecentlyAddedComparer
            Implements Collections.Generic.IComparer(Of Post)

            Public Function Compare(ByVal x As Post, ByVal y As Post) As Integer Implements System.Collections.Generic.IComparer(Of Post).Compare
                Return Date.Compare(x.DateAdded, y.DateAdded)
            End Function

        End Class
        Public Class SortByMostRecentlyUpdatedComparer
            Implements Collections.Generic.IComparer(Of Post)

            Public Function Compare(ByVal x As Post, ByVal y As Post) As Integer Implements System.Collections.Generic.IComparer(Of Post).Compare
                Return Date.Compare(x.DateUpdated, y.DateUpdated)
            End Function

        End Class
        Public Class SortByOldestAddedComparer
            Implements Collections.Generic.IComparer(Of Post)

            Public Function Compare(ByVal x As Post, ByVal y As Post) As Integer Implements System.Collections.Generic.IComparer(Of Post).Compare
                Return Date.Compare(y.DateAdded, x.DateAdded)
            End Function

        End Class
        Public Class SortByIdComparer
            Implements Collections.Generic.IComparer(Of Post)

            Public Function Compare(ByVal x As Post, ByVal y As Post) As Integer Implements System.Collections.Generic.IComparer(Of Post).Compare
                If x.Id < y.Id Then Return -1
                If x.Id = y.Id Then Return 0
                If x.Id > y.Id Then Return 1
            End Function

        End Class

        'Functions

        Public Function ToRss20Item() As Common.Rss.Version20.Item
            Dim _return As New Common.Rss.Version20.Item
            _return.Author = Common.Data.SiteAuthor
            _return.Category = Common.Data.SiteCategory
            _return.Title = Me.Title
            _return.Description = Me.Body
            _return.PublicationDate = Me.DateUpdated
            Return _return
        End Function

    End Class
End Namespace